package xin.lhuo.we.server.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xin.lhuo.we.server.dao.AlbumMapper;
import xin.lhuo.we.server.dao.PhotoMapper;
import xin.lhuo.we.server.entity.Album;
import xin.lhuo.we.server.enums.ResultEnum;
import xin.lhuo.we.server.exception.WeException;
import xin.lhuo.we.server.properties.QiNiuProperties;
import xin.lhuo.we.server.service.IAlbumService;
import xin.lhuo.we.server.utils.QiNiuUtil;

import java.util.List;

import static xin.lhuo.we.server.enums.ResultEnum.*;

/**
 * @author Lhuo
 * @date 2019/5/11 19:34
 */
@Slf4j
@Service(value = "albumServer")
public class AlbumServiceImpl implements IAlbumService {

    private final AlbumMapper albumMapper;

    private final PhotoMapper photoMapper;

    private final QiNiuProperties qiNiuProperties;

    @Autowired
    public AlbumServiceImpl(AlbumMapper albumMapper, PhotoMapper photoMapper, QiNiuProperties qiNiuProperties) {
        this.albumMapper = albumMapper;
        this.photoMapper = photoMapper;
        this.qiNiuProperties = qiNiuProperties;
    }

    @Override
    public void createAlbum(Album album,Integer userId) {
        if (albumMapper.checkName(album.getName(),userId) > 0) {
            throw new WeException(ResultEnum.PARAM_ERROR.getCode(), "相册名字已存在");
        }
        try {
            albumMapper.save(album);
        } catch (Exception e) {
            e.printStackTrace();
            throw new WeException(ResultEnum.DB_UPDATE_ERROR);
        }

    }

    @Override
    public List<Album> getAlbums(Integer userId) {
        try {
            List<Album> albums = albumMapper.selectAllByUserId(userId);
            for (Album album : albums) {
                if (!StringUtils.isEmpty(album.getCoverUrl())) {
                    album.setCoverUrl(QiNiuUtil.getDownloadUrl(qiNiuProperties, album.getCoverUrl()));
                }
            }
            return albums;
        } catch (Exception e) {
            e.printStackTrace();
            throw new WeException(DB_SELECT_ERROR);
        }
    }

    @Override
    public void deleteAlbum(Integer id, Integer userId) {
        if (albumMapper.checkByUserIdAndId(id, userId) == 0) {
            throw new WeException(DB_DELETE_ERROR.getCode(), "相册不存在");
        }

        if (photoMapper.checkPhotoByAlbumId(id) > 0) {
            throw new WeException(DB_DELETE_ERROR.getCode(), "相册不是空的，不可以删除");
        }
        try {
            Album album = albumMapper.selectById(id);
            albumMapper.deleteById(id);
            if (!StringUtils.isEmpty(album.getCoverKey())){
                QiNiuUtil.deleteFile(qiNiuProperties,album.getCoverKey());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new WeException(DB_DELETE_ERROR);
        }
    }

    @Override
    public void rename(Integer id, String name, Integer userId) {
        if (albumMapper.checkByUserIdAndId(id, userId) == 0) {
            throw new WeException(DB_DELETE_ERROR.getCode(), "相册不存在");
        }
        if (albumMapper.checkName(name,userId) > 0) {
            throw new WeException(PARAM_ERROR.getCode(), "相册已存在");
        }
        albumMapper.rename(id, name);
    }

    @Override
    public String getUploadAlbumToken() {
        return QiNiuUtil.getUpTokenNoCallBack(qiNiuProperties);
    }

    @Override
    public void updateAlbumCoverUrl(Integer id, String key, Integer userId) {
        String url = qiNiuProperties.getBaseUrl() + key;
        albumMapper.updateCoverUrlById(id, url, key,userId);
    }


}
